
"use strict";
/* ========================================================
		BACKGROUND ZOOM EXPERIMENT OXP

Created by Karl Smith ("Wildeblood"), July, 2024.
=========================================================== */

    this.name    = "zoom";
    this.version = "a 1.0";

/* ========================================================
		BACKGROUND ZOOM EXPERIMENT OXP

 1. For this demonstration, we only use the market screen.
 2. On entering the screen, we run a 32-step zoom-out animation.
 3. Because we use one image, and only change the scaling factor,
    we cunningly avoid stuttering caused by loading image files.
 3a. It will look cool. Trust me, Bro.

            ASPIRATIONAL ROADMAP

 4. Alpha 1: Use 0.25s timer to create 8 second animation.
 5. Alpha 2: Use FCB to average 24 updates per second,
    giving 1.3 second animation.
 6. Review time. DECISION: Continue, or abandon idea.
 7. Beta: Extend to other screens.
 8. Post-1: Replace linear zoom function with J-shaped function,
    i.e. zoomStep not fixed at 16.
=========================================================== */

    this._zoomStep = 16;    // 32 x 16 = 512.
    this._zoomMax = 1024;
    this._zoomMin = 512;
    this._zoomImage = "zoom_zoomable_market.png"; // Name of current image file. Will always be market for alpha script.

 // These next 3 are used while an animation is in progress...

    this._zooming = false;  // true while the animation is running.
    this._zoomSize = null;  // Varies while the animation is running.
    this._zoomName = null;  // Holds the ID of the timer or frame callback while it's running.

    this.startUpComplete = function () {
        if (player.ship.docked) {
            this.shipDockedWithStation(player.ship.dockedStation);
        }
    }

    this.shipDockedWithStation = function (station) {
     // Reset all backgrounds to fully zoomed-in, i.e. height: 1024...
        setScreenBackgroundForKey("market", { name: "zoom_zoomable_market.png", height: 1024 });
    }

    this.guiScreenChanged = function (to, from) {
        if (this._zooming) {
         // Abort!
            this._zooming = false;
            if (this._zoomName) {
                this._zoomName.stop();
                delete this._zoomName;
            }
         // reset screen background for key to height 1024.
        setScreenBackgroundForKey("market", { name: "zoom_zoomable_market.png", height: 1024 });
        }
        if (to == "GUI_SCREEN_MARKET" && player.ship.docked && guiScreen == "GUI_SCREEN_MARKET") {
            this._zoomName = new Timer(this, _zoomOut.bind(this), 0, 0.25);
        }
    }

    this._zoomOut = function () {
        if (this._zooming) {
         // We've already started; just advance one step.
            this._zoomSize -= this._zoomStep;
            setScreenBackground({ name: (this._zoomImage), height: (this._zoomSize) });
         // Then check if we've reached the end...
            if (this._zoomSize <= this._zoomMin) {
                this._zooming = false;
                if (this._zoomName) {
                    this._zoomName.stop();
                    delete this._zoomName;
                }
             // reset screen background for key to height 1024 ?
            }
        } else {
         // Let's get started...
            this._zoomImage = getScreenBackgroundForKey("market");
            log(this.name, this._zoomImage)
            this._zoomImage = "zoom_zoomable_market.png";
            this._zooming = true;
            this._zoomSize = this._zoomMax;
            setScreenBackground({ name: (this._zoomImage), height: (this._zoomSize) });
        }
    }

/* ========================================================
			THE END
=========================================================== */